package com.gupao.study.design.singleton.lazy;

/**
 * 懒汉式，性能好的线程不安全，线程安全的性能差
 * @author asus
 *
 */
public class Lazy {
	private static volatile Lazy lazy = null;
	private Lazy() {
		
	}
	
	/**
	 * 线程不安全
	 * @return
	 */
	public static Lazy getInstance() {
		if(lazy==null) {
			lazy = new Lazy();
		}
		
		return lazy;
	}
	/**
	 * 线程安全，但是性能很差
	 * @return
	 */
	public synchronized static Lazy getInstanceSafe() {
		if(lazy==null) {
			lazy = new Lazy();
		}
		
		return lazy;
	}
	/**
	 * 线程安全，性能稍微好一点
	 * @return
	 */
	public static Lazy getInstanceSafeDoubleCheck() {
		if(lazy==null) {
			synchronized(Lazy.class) {
				if(lazy==null) {
					lazy = new Lazy();
				}
			}
		}
		return lazy;
	}
	
}
